package com.lfg.leetcode.leetcode300;


public class No335isSelfCrossing {
    public static void main(String[] args) {
        System.out.println(isSelfCrossing(new int[]{1, 1, 2, 1, 1}));
        System.out.println(isSelfCrossing(new int[]{1, 2, 2, 3, 4}));
        System.out.println(isSelfCrossing(new int[]{2, 1, 3, 3, 2, 2}));
        System.out.println(isSelfCrossing(new int[]{1, 1, 2, 2, 3, 3, 4, 4, 10, 4, 4, 3, 3, 2, 2, 1, 1}));
    }

    public static boolean isSelfCrossing(int[] x) {
        if (x.length < 4) {
            return false;
        }
        for (int i = 3; i < x.length; i++) {
            if (x[i - 1] <= x[i - 3] && x[i] >= x[i - 2]) {
                return true;
            }
            if (i >= 4 && x[i - 2] == (x[i - 4] + x[i]) && x[i - 1] == x[i - 3]) {
                return true;
            }
            if (i >= 5 && x[i - 2] >= x[i - 4] && x[i - 3] >= x[i - 1] && x[i - 2] <= (x[i - 4] + x[i]) && x[i - 1] + x[i - 5] >= x[i - 3]) {
                return true;
            }
        }
        return false;
    }

}



